package com.weichat.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.model.Wx_access_token;
import com.service.UserService;
import com.service.Wx_access_tokenService;
import com.serviceImp.UserServiceImp;
import com.serviceImp.Wx_access_tokenServiceImp;
import com.weichat.accessToken.Validate;
import com.weichat.tool.AccessTokentool;
import com.weichat.tool.Sha1Util;
import com.weichat.tool.WeiChatConfig;
import com.weichat.tool.WeiChatUtil;

@RestController
public class WebController {
	
	@Autowired
	UserService userService;
	@Autowired
	Wx_access_tokenService wx_access_tokenService;
	@Autowired
	AccessTokentool accessTokentool;

	@RequestMapping("/iniConfig")
	public Map<String,String> getJSDKConfig(@RequestParam("url") String url) throws Exception{
		
		
		
		
		Map<String,String> configParam=new HashMap<String,String>();
		SortedMap<String, String> finalpackage = new TreeMap<String, String>();
		String timestamp = Sha1Util.getTimeStamp();
		String NonceStr=Sha1Util.getNonceStr();
		WeiChatUtil wu=new WeiChatUtil();
	   //WeiChatConfig.accesstoken=wu.getAccessToken();
		//String accesstoken=wu.getAccessToken();
		String accesstoken=(String) accessTokentool.getNewAccessToken().get("rs");
		System.out.println("accesstoken======"+accesstoken);
		String Ticket;
		try {
			Ticket = Sha1Util.getjsApiTicket("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=",accesstoken);
			System.out.println("timestamp========"+timestamp);
			System.out.println("NonceStr======"+NonceStr);
			System.out.println("Ticket======="+Ticket);
			finalpackage.put("timestamp", timestamp);
			finalpackage.put("noncestr", NonceStr);
			finalpackage.put("jsapi_ticket", Ticket);
			finalpackage.put("url", url);
			System.out.println("url--------"+url);
			String signature =Sha1Util.createSHA1Sign(finalpackage);
			System.out.println("signature=="+signature);
			
			configParam.put("appId", WeiChatConfig.appid);
			configParam.put("timestamp", timestamp);
			configParam.put("nonceStr", NonceStr);
			configParam.put("signature", signature);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//finalpackage.put("appId", WeiChatConfig.appid);
		
		return configParam;
	}
	
	@RequestMapping("chooseWXPayo")
	public Map<String,String> postchooseWXPay(HttpServletRequest request){
		
		
		
		Wx_access_token record =new Wx_access_token();
		record.setAccessToken("123123");
		record.setExpiresIn(123);
		System.out.println("ve---------"+userService.selectByPrimaryKey(126).getName());
		wx_access_tokenService.insertSelective(record);
		System.out.println("ve---------"+wx_access_tokenService.selectByPrimaryKey(1).getAccessToken());

		Map<String,String> postWX=new HashMap<String,String>();
		postWX.put("appId",WeiChatConfig.appid);
		postWX.put("timeStamp",WeiChatUtil.getTimeStamp());// 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
		postWX.put("nonceStr",(String) request.getSession().getAttribute("nonce_str"));// 支付签名随机串，不长于 32 位
		postWX.put("package",(String) request.getSession().getAttribute("prepay_id"));// 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）■██████
		postWX.put("signType","MD5");// 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
		postWX.put("paySign",(String) request.getSession().getAttribute("pay_sign"));// 支付签名*/■██████

		return postWX;
	}
	@RequestMapping("test")
	public Map<String,String> test(HttpServletRequest request) throws IOException{
		
		
		System.out.println(accessTokentool.getNewAccessToken());
		

		Map<String,String> postWX=new HashMap<String,String>();
		
		return postWX;
	}

}
